package com.lanou3g.code0510;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/10;
 * Otherwise, I don't know who created it either
 */

@WebServlet(name = "DownloadServlet",urlPatterns = "/down")
public class DownloadServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应的编码格式为 utf8
        response.setCharacterEncoding("utf8");

        //设置下载响应头
        response.setHeader("Content-Disposition",
                "attachment;filename=%e5%a7%90%e5%a7%90.mp3");
        InputStream is = getServletContext()
                .getResourceAsStream("files/姐姐.mp3");
        //向浏览器 写流
        response.getOutputStream().write(IOUtils.toByteArray(is));
    }
}
